package com.yxch.admin.service.impl.home;

import com.yxch.admin.mapper.home.HomeMapper;
import com.yxch.admin.model.commen.QsPoint;
import com.yxch.admin.model.home.*;
import com.yxch.admin.model.commen.Direction;
import com.yxch.admin.model.commen.DirectionVO;
import com.yxch.admin.model.commen.Function;
import com.yxch.admin.service.home.HomeService;
import com.yxch.admin.utils.AliOSSUtils;
import com.yxch.admin.utils.ResultAPI;
import com.yxch.admin.utils.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.yxch.admin.utils.ExceptionEnum.ADDTEACH_ERR;

@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    HomeMapper homeMapper;
    @Autowired
    AliOSSUtils aliOSSUtils;

    @Override
    public ResultAPI getCourseIntro(Integer courseId) {
        CourseIntro courseIntro = homeMapper.getCourseIntro(courseId);
        return ResultAPI.success(courseIntro);
    }

    @Override
    public ResultAPI updateCourseIntro(CourseIntro courseIntro) {
        homeMapper.updateCourseIntro(courseIntro);
        return ResultAPI.success();
    }

    @Override
    public ResultAPI getTeam(Integer courseId) {
        List<TeacherTeam> teacherTeamList = homeMapper.getTeam(courseId);
        return ResultAPI.success(teacherTeamList);
    }


    @Override
    public ResultAPI deleteTeamTeachers(List<Integer> ids) {
        homeMapper.deleteTeamTeachers(ids);
        return ResultAPI.success();
    }

    @Transactional
    @Override
    public ResultAPI addandUpdateTeamTeacher(TeacherTeam teacherTeam) {
        if (teacherTeam.getTech_cour_id() != null) {
            homeMapper.updateTeam(teacherTeam);
        } else {
            //根据电话号码获取user_id
            Integer id = homeMapper.getIdByPhone(teacherTeam.getPhone_num());
            if (id == null) {
                return ResultAPI.error("请输入正确的老师电话号码！");
            }
            teacherTeam.setUser_id(id);
            homeMapper.addTeam(teacherTeam);
        }

        return ResultAPI.success();
    }

    /**
     * 获取话题考试方向
     *
     * @return
     */
    public ResultAPI getDirectionLsit(Integer direction_id) {
        DirectionVO directionList = homeMapper.getDirection(direction_id);
        return ResultAPI.success(directionList);
    }

    /**
     * 获取题库知识点
     *
     * @return
     */
    public ResultAPI getQspoint(Integer direction_id) {
        List<Direction> directionList = homeMapper.getQspoint(direction_id);
        return ResultAPI.success(directionList);
    }

    public ResultAPI getFunction() {
        List<Function> functions = homeMapper.getFunction();
        return ResultAPI.success(functions);
    }

    @Override
    public ResultAPI getHomeBaseData() {
        HomeData homeData = homeMapper.getHomeBaseData();
        return ResultAPI.success(homeData);
    }

    @Override
    public ResultAPI getDaysData() {
        Map loginMap = homeMapper.getLoginDaysData();
        Map commentMap = homeMapper.getCommentDaysData();

        DaysData daysData = new DaysData();
        daysData.setCommentData(commentMap);
        daysData.setLoginData(loginMap);
        return ResultAPI.success(daysData);
    }

    @Override
    public ResultAPI getBingData() {
        List<List<BingData>> res = new ArrayList<>();
        List<BingData> userType = homeMapper.countUsersByType();
        List<BingData> gender = homeMapper.countUsersByGender();
        res.add(userType);
        res.add(gender);
        return ResultAPI.success(res);
    }

    @Override
    public ResultAPI getPointData() {
        List<PointData> pointDatas = homeMapper.countQuestionsByPoint();
        return ResultAPI.success(pointDatas);
    }

    @Override
    public ResultAPI addandUpdateFunction(Function function) {
        if (function.getFunction_id() != null) {
            homeMapper.updateFunction(function);
        } else {
            homeMapper.addFunction(function);
        }
        return ResultAPI.success();
    }

    @Override
    public ResultAPI deleteFunction(List<Integer> ids) {
        homeMapper.deleteFunction(ids);
        return ResultAPI.success();
    }

    @Override
    public ResultAPI addPoint(QsPoint qsPoint) {
        homeMapper.addPoint(qsPoint);
        return ResultAPI.success();
    }
}
